﻿using System;
using System.Web.Mvc;
using Web7.Container.Controllers;
using Web7.Core.ActionFilters.Authorization;
using Web7.Core.Apps;
using Web7.FilterProviders;
using Web7.FilterProviders.FilterCriterion;
using Web7.Core.Security;
using System.Collections.Generic;

namespace Web7.Container
{
    public class ContainerApp : App
    {
        /// <summary>
        /// 应用名称
        /// </summary>
        public override string AppName
        {
            get { return "Container"; }
        }

        /// <summary>
        /// 应用鉴定名称
        /// </summary>
        public override string AuthenticationAppName
        {
            get { return AppName; }
        }

        /// <summary>
        /// 路由注册
        /// </summary>
        public override void RegisterRoutes()
        {
            // 用于匹配 Guid 的正则表达式
            string guidRegex = @"^[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}$";

            #region L10N

            MapRoute("Language",
                     "Language",
                     new { controller = "Language", action = "Index" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Language.List",
                     "Language/List",
                     new { controller = "Language", action = "List" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Language.Create",
                     "Language/Create",
                     new { controller = "Language", action = "Create" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Language.Edit",
                     "Language/Edit/{id}",
                     new { controller = "Language", action = "Edit" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Language.Resources",
                 "Language/Resources/{languageId}",
                 new { controller = "Language", action = "Resources" },
                null,
                 new[] { "Web7.Container.Controllers" });

            MapRoute("Language.ResourcesCreate",
                 "Language/ResourcesCreate/{languageId}",
                 new { controller = "Language", action = "ResourcesCreate" },
                null,
                 new[] { "Web7.Container.Controllers" });


            #endregion

            List<string> list = new List<string>();
            foreach (var item in Enum.GetValues(typeof(PermissionOwnerType)))
            {
                list.Add(item.ToString());
            }

            MapRoute("Admin.Permission.Rights",
                 "Admin/Rights/{OwnerType}/{id}",
                 new { controller = "PermissionRecord", action = "Index" },
                 new { OwnerType = string.Join("|", list), id = guidRegex },
                 new[] { "Web7.Container.Controllers" });

            #region ROLE

            MapRoute("Admin.Role.Default",
                     "Admin/Role/",
                     new { controller = "Role", action = "Index" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.Role.Create",
                     "Admin/Role/Create",
                     new { controller = "Role", action = "Create" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.Role.Edit",
                     "Admin/Role/Edit/{id}",
                     new { controller = "Role", action = "Edit" },
                     new { id = guidRegex },
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.Role.Delete",
                     "Admin/Role/Delete/{id}",
                     new { controller = "Role", action = "Delete" },
                     new { id = guidRegex },
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.Role.Bad",
                     "Admin/Role/Bad/{extype}",
                     new { controller = "Role", action = "Bad" },
                     null,
                     new[] { "Web7.Container.Controllers" });
            #endregion

            #region User

            MapRoute("Admin.User.Default",
                     "Admin/User/",
                     new { controller = "User", action = "Index" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.User.Create",
                     "Admin/User/Create",
                     new { controller = "User", action = "Create" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.User.Edit",
                     "Admin/User/Edit/{id}",
                     new { controller = "User", action = "Edit" },
                     new { id = guidRegex },
                     new[] { "Web7.Container.Controllers" });

            MapRoute("Admin.User.Delete",
                     "Admin/User/Delete/{id}",
                     new { controller = "User", action = "Delete" },
                     new { id = guidRegex },
                     new[] { "Web7.Container.Controllers" });

            #endregion

            MapRoute("Default",
                    "",
                    new { controller = "Front", action = "Entry" },
                    null,
                    new[] { "Web7.Container.Controllers" });

            MapRoute("Error",
                    "Error/{code}",
                    new { controller = "Error", action = "Index" },
                    null,
                    new[] { "Web7.Container.Controllers" });

            MapRoute("Front.CultureChange",
                     "Front/CultureChange",
                     new { controller = "Front", action = "CultureChange" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("UserCenter",
                     "UserCenter",
                     new { controller = "UserCenter", action = "Index" },
                     null,
                     new[] { "Web7.Container.Controllers" });


            MapRoute("NoRight",
                     "NoRight",
                     new { controller = "Front", action = "NoRight" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("User.LogOn",
                     "LogOn",
                     new { controller = "Front", action = "LogOn" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("User.LogIn",
                     "LogIn",
                     new { controller = "User", action = "LogIn" },
                     null,
                     new[] { "Web7.Container.Controllers" });

            MapRoute("User.LogOut",
                     "LogOut",
                     new { controller = "User", action = "LogOut" },
                     null,
                     new[] { "Web7.Container.Controllers" });
        }

        /// <summary>
        /// 基本权限设置
        /// </summary>
        /// <param name="filterRegistry"></param>
        /// <param name="globalFilters"></param>
        public override void RegisterFilters(FilterRegistryFilterProvider filterRegistry, GlobalFilterCollection globalFilters)
        {
            //参考
            ControllerActionFilterCriteria userManager = new ControllerActionFilterCriteria();
            userManager.AddMethod<UserController>(userController => userController.Index(null));
            filterRegistry.Add(new[] { userManager }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.ManageUser)));

            ControllerActionFilterCriteria userEdit = new ControllerActionFilterCriteria();
            userEdit.AddMethod<UserController>(userController => userController.Edit(new Guid()));
            filterRegistry.Add(new[] { userEdit }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.UserEdit)));

            ControllerActionFilterCriteria userCreate = new ControllerActionFilterCriteria();
            userCreate.AddMethod<UserController>(userController => userController.Create());
            filterRegistry.Add(new[] { userCreate }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.UserCreate)));

            ControllerActionFilterCriteria userDelete = new ControllerActionFilterCriteria();
            userDelete.AddMethod<UserController>(userController => userController.Delete(new Guid()));
            filterRegistry.Add(new[] { userDelete }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.UserDelete)));

            ControllerActionFilterCriteria roleManager = new ControllerActionFilterCriteria();
            roleManager.AddMethod<RoleController>(roleController => roleController.Index(null));
            filterRegistry.Add(new[] { roleManager }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.ManageRole)));

            ControllerActionFilterCriteria roleEdit = new ControllerActionFilterCriteria();
            roleEdit.AddMethod<RoleController>(roleController => roleController.Edit(new Guid()));
            filterRegistry.Add(new[] { roleEdit }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.RoleEdit)));

            ControllerActionFilterCriteria roleCreate = new ControllerActionFilterCriteria();
            roleCreate.AddMethod<RoleController>(roleController => roleController.Create());
            filterRegistry.Add(new[] { roleCreate }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.RoleCreate)));

            ControllerActionFilterCriteria roleDelete = new ControllerActionFilterCriteria();
            roleDelete.AddMethod<RoleController>(roleController => roleController.Delete(new Guid()));
            filterRegistry.Add(new[] { roleDelete }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.RoleDelete)));


            //ControllerActionFilterCriteria permissionRecords = new ControllerActionFilterCriteria();
            //permissionRecords.AddMethod<PermissionRecordController>(controller => controller.Index());
            //filterRegistry.Add(new[] { permissionRecords }, new AdvancedAuthorizationFilter(DependencyResolverContext, authorizer => authorizer.Authorize(Permissions.PermissionRecords)));
        }
    }
}
